﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.ZP;
using Rison.BusinessFacade.Helper;
using Rison.DataAccess.Common;
using Rison.DataAccess.Data.ZP;

namespace Rison.BusinessFacade.Facade.ZP
{
    public sealed class SelfExaminationFacade : FacadeBase
    {
        protected override DataAccessBase dbObject
        {
            get
            {
                if (dbHelper == null)
                    dbHelper = new SelfExaminationDB();

                return dbHelper;
            }
        }

       
        protected override string keyFieldName
        {
            get
            {
                return "ID";
            }
        }
              
        protected override List<SqlField> GetSqlFieldCollection(object value, bool getIdentityField)
        {
            List<SqlField> fields = new List<SqlField>();

            if (getIdentityField)
            {
                fields.Add(new SqlField("ID", (value as SelfExamination).ID, true, true));
                fields.Add(new SqlField("Year", (value as SelfExamination).Year, true, false));
                fields.Add(new SqlField("DepartID", (value as SelfExamination).DepartID, true, true));
            }

            else
            {
                fields.Add(new SqlField("ID", (value as SelfExamination).ID));
                fields.Add(new SqlField("Year", (value as SelfExamination).Year));
                fields.Add(new SqlField("DepartID", (value as SelfExamination).DepartID));
            }

            fields.Add(new SqlField("FieldTotal", (value as SelfExamination).FieldTotal));
            fields.Add(new SqlField("FieldNum", (value as SelfExamination).FieldNum));

            fields.Add(new SqlField("CompanyName", (value as SelfExamination).CompanyName));
            fields.Add(new SqlField("ZGKS", (value as SelfExamination).ZGKS));
            fields.Add(new SqlField("FGLD", (value as SelfExamination).FGLD));
            fields.Add(new SqlField("FGLDTelephone", (value as SelfExamination).FGLDTelephone));
            fields.Add(new SqlField("FGLDMobile", (value as SelfExamination).FGLDMobile));
            fields.Add(new SqlField("FGLDEmail", (value as SelfExamination).FGLDEmail));
            fields.Add(new SqlField("WZFZR", (value as SelfExamination).WZFZR));
            fields.Add(new SqlField("WZFZRTelephone", (value as SelfExamination).WZFZRTelephone));
            fields.Add(new SqlField("WZFZRMobile", (value as SelfExamination).WZFZRMobile));
            fields.Add(new SqlField("WZFZREmail", (value as SelfExamination).WZFZREmail));
            fields.Add(new SqlField("AQLLR", (value as SelfExamination).AQLLR));
            fields.Add(new SqlField("AQLLRTelephone", (value as SelfExamination).AQLLRTelephone));
            fields.Add(new SqlField("AQLLRMobile", (value as SelfExamination).AQLLRMobile));
            fields.Add(new SqlField("AQLLREmail", (value as SelfExamination).AQLLREmail));

            fields.Add(new SqlField("WebSite", (value as SelfExamination).WebSite));
            fields.Add(new SqlField("WebFZR", (value as SelfExamination).WebFZR));
            fields.Add(new SqlField("WebFZRTelephone", (value as SelfExamination).WebFZRTelephone));
            fields.Add(new SqlField("WebFZRMobile", (value as SelfExamination).WebFZRMobile));
            fields.Add(new SqlField("WebFZREmail", (value as SelfExamination).WebFZREmail));
            fields.Add(new SqlField("WebTBR", (value as SelfExamination).WebTBR));
            fields.Add(new SqlField("WebTBRTelephone", (value as SelfExamination).WebTBRTelephone));
            fields.Add(new SqlField("WebTBRMobile", (value as SelfExamination).WebTBRMobile));
            fields.Add(new SqlField("WebTBREmail", (value as SelfExamination).WebTBREmail));
            fields.Add(new SqlField("IfZZRY", (value as SelfExamination).IfZZRY));
            fields.Add(new SqlField("WebHosting", (value as SelfExamination).WebHosting));
            fields.Add(new SqlField("WebHostingDes", (value as SelfExamination).WebHostingDes));
            fields.Add(new SqlField("IFSubSite", (value as SelfExamination).IFSubSite));
            fields.Add(new SqlField("FirstDomain", (value as SelfExamination).FirstDomain));
            fields.Add(new SqlField("RecordNum", (value as SelfExamination).RecordNum));
            fields.Add(new SqlField("SecondDomain", (value as SelfExamination).SecondDomain));
            fields.Add(new SqlField("CreateTime", (value as SelfExamination).CreateTime));
            fields.Add(new SqlField("Creator", (value as SelfExamination).Creator));
            fields.Add(new SqlField("LastModifiedTime", (value as SelfExamination).LastModifiedTime));
            fields.Add(new SqlField("LastModified", (value as SelfExamination).LastModified));
            fields.Add(new SqlField("State", (value as SelfExamination).State));


            return fields;
        }

        protected override object SetPropertiesValue(DataRow row)
        {
            SelfExamination obj = new SelfExamination();
            obj.ID = row["id"].ToString();
            obj.Year = Convert.ToInt32(row["Year"].ToString());
            obj.DepartID = StringHelper.GetValue(row, "DepartID");
            obj.FieldTotal = Convert.ToInt32(row["FieldTotal"]);
            obj.FieldNum = Convert.ToInt32(row["FieldNum"]);

            obj.CompanyName = StringHelper.GetValue(row, "CompanyName");
            obj.ZGKS = StringHelper.GetValue(row, "ZGKS");
            obj.FGLD = StringHelper.GetValue(row, "FGLD");
            obj.FGLDTelephone = StringHelper.GetValue(row, "FGLDTelephone");
            obj.FGLDMobile = StringHelper.GetValue(row, "FGLDMobile");
            obj.FGLDEmail = StringHelper.GetValue(row, "FGLDEmail");
            obj.WZFZR = StringHelper.GetValue(row, "WZFZR");
            obj.WZFZRTelephone = StringHelper.GetValue(row, "WZFZRTelephone");
            obj.WZFZRMobile = StringHelper.GetValue(row, "WZFZRMobile");
            obj.WZFZREmail = StringHelper.GetValue(row, "WZFZREmail");
            obj.AQLLR = StringHelper.GetValue(row, "AQLLR");
            obj.AQLLRTelephone = StringHelper.GetValue(row, "AQLLRTelephone");
            obj.AQLLRMobile = StringHelper.GetValue(row, "AQLLRMobile");
            obj.AQLLREmail = StringHelper.GetValue(row, "AQLLREmail");

            obj.WebSite = StringHelper.GetValue(row, "WebSite");
            obj.WebFZR = StringHelper.GetValue(row, "WebFZR");
            obj.WebFZRTelephone = StringHelper.GetValue(row, "WebFZRTelephone");
            obj.WebFZRMobile = StringHelper.GetValue(row, "WebFZRMobile");
            obj.WebFZREmail = StringHelper.GetValue(row, "WebFZREmail");
            obj.WebTBR = StringHelper.GetValue(row, "WebTBR");
            obj.WebTBRTelephone = StringHelper.GetValue(row, "WebTBRTelephone");
            obj.WebTBRMobile = StringHelper.GetValue(row, "WebTBRMobile");
            obj.WebTBREmail = StringHelper.GetValue(row, "WebTBREmail");
            obj.IfZZRY = StringHelper.GetValue(row, "IfZZRY");
            obj.WebHosting = StringHelper.GetValue(row, "WebHosting");
            obj.WebHostingDes =StringHelper.GetValue(row, "WebHostingDes");
            obj.FirstDomain = StringHelper.GetValue(row, "FirstDomain");
            obj.RecordNum = StringHelper.GetValue(row, "RecordNum");
            obj.SecondDomain = StringHelper.GetValue(row, "SecondDomain");
            obj.IFSubSite = StringHelper.GetValue(row, "IFSubSite");
            obj.CreateTime = StringHelper.GetValue(row, "CreateTime");
            obj.Creator = StringHelper.GetValue(row, "Creator");
            obj.LastModifiedTime = StringHelper.GetValue(row, "LastModifiedTime");
            obj.LastModified = StringHelper.GetValue(row, "LastModified");
            obj.State = StringHelper.GetValue(row, "State");

            return obj;
        }        

        public string GetID(string departid, int year)
        {
            DataRow dr = (dbObject as SelfExaminationDB).GetID(departid, year);
            if (dr !=null)
              return dr["ID"].ToString();

            return "";
        }

        public DataRow GetInformation(string departid, int year)
        {
            DataRow dr = (dbObject as SelfExaminationDB).GetInformation(departid, year);
            return dr;
        }

        public DataRow GetInformation(string id)
        {
            if (id != "")
            {
                DataRow dr = (dbObject as SelfExaminationDB).GetInformation(id);
                return dr;
            }
            else
                return null;
        }

        //是否提交
        public string isExistSelfExam(string departid, int year)
        {
            DataRow dr=GetInformation(departid,year);
            if (dr!=null)
            {
                if (dr["state"].ToString() == "1")
                {
                    return "已提交";
                }
                return "填报中";
            }

            return "未填报";
        }


        //完成率
        public int getWCL(string departid, int year)
        {
            try
            {
                DataRow dr = GetInformation(departid, year);
                if (dr != null)
                {
                    return Convert.ToInt16(dr["FieldNum"].ToString()) * 100 / Convert.ToInt16(dr["FieldTotal"].ToString());
                }
            }
            catch
            {
            }

            return 0;
        }

        #region 处理子网站

        public DataTable LoadSubInfobyParentId(string parentId)
        {
            if (parentId == "")
                return null;

            string tableName = "SelfExam_SubWeb";                  

            DataSet ds=(dbObject as SelfExaminationDB).GetSubInfoByParentID(tableName, parentId);

            if (ds == null)
                return null;

            return ds.Tables[0];

        }
 
        public string SaveSubInfobyParentId(string parentId,string param)
        {
            if (parentId == "")
                return "";

            string tableName = "SelfExam_SubWeb";
            int returnValue = -1;           

            try
            {
                returnValue = DeleteSubInfobyParentId(tableName, parentId);

                returnValue = InsertSubInfobyParentId(tableName, parentId, param);
            }

            catch
            {

            }

            if(returnValue<0)
                return "更新子表出错！";

            return "";
        }

         /// <summary>
        /// 删除子表信息。
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public int DeleteSubInfobyParentId(string tableName,string parentId)
        {
            if (parentId != "")
            {
                return (dbObject as SelfExaminationDB).DeleteSubInfobyParentId(tableName, parentId);
            }
            return 0;
        }

        /// <summary>
        /// 插入子表信息。
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public int InsertSubInfobyParentId(string tableName,string parentId,string con)
        {            
            if (con!=null && con!="")
            {
                int returnValue = -1;
                string[] a = con.Split(',');
                int rowCount = a.Length / 2;

                for (int i = 0; i < rowCount; i++ )
                {
                    StringBuilder param = new StringBuilder();

                    param.Append("newid(),");
                    param.Append("'" + parentId + "',");
                    param.Append("'" + a[i * 2].ToString() + "',");
                    param.Append("'" + a[i * 2 + 1 ].ToString() + "',");
                    param.Append("'" + (i+1).ToString() + "'");
                     
                    returnValue = (dbObject as SelfExaminationDB).InsertSubInfobyParentId(tableName, param.ToString());
                }
                return returnValue;
            }
            return 0;
            
        }

        #endregion

        #region
        /// <summary>
        /// 保存工作人员
        /// </summary>
        /// <param name="efList"></param>
        /// <returns></returns>
        public string SaveEmployeeFieldCollection(List<EmployeeField> efList, string parentId)
        {
            if (parentId == "")
                return "";

            int returnValue = -1;
            string tableName = "SelfExam_Employee";
            StringBuilder param = new StringBuilder();

            try
            {
                returnValue = DeleteSubInfobyParentId(tableName, parentId);

                //table循环
                foreach (EmployeeField a in efList)
                {
                    if (a.Value!=null && a.Value != "")
                    {
                        string type = a.Name;
                        string[] value = a.Value.Split(',');
                        int rowCount = value.Length / 4;

                        for(int m=0; m<rowCount; m++)
                        {
                            param = new StringBuilder();
                            param.Append("newid(),");
                            param.Append("'" + parentId + "',");
                            param.Append("'" + type + "',");
                            param.Append("'" + value[m * 4].ToString() + "',");
                            param.Append("'" + value[m * 4 + 1].ToString() + "',");
                            param.Append("'" + value[m * 4 + 2].ToString() + "',");
                            param.Append("'" + value[m * 4 + 3].ToString() + "',");
                            param.Append("'" + (m + 1).ToString() + "'");

                            returnValue = (dbObject as SelfExaminationDB).InsertSubInfobyParentId(tableName, param.ToString());
                        }
                        
                    }                   

                }

            }
            catch
            {

            }

            if (returnValue < 0)
                return "更新子表出错！";

            return "";
        }

        public DataTable LoadEmployeebyParentID(string parentId)
        {
            if(parentId!="")
            {
                string tableName = "SelfExam_Employee";                  

                DataSet ds=(dbObject as SelfExaminationDB).GetSubInfoByParentID(tableName, parentId);
                if (ds != null && ds.Tables.Count> 0)
                    return ds.Tables[0];                
            }

            return null;
        }

        #endregion
    }
}
